package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @RequestMapping("/page")
    public R<Page<Category>> selectAll(Integer page,Integer pageSize){
        Page<Category> categoryPage = categoryService.selectByPage(page, pageSize);

        R<Page<Category>> success = R.success(categoryPage);
        return success;
    }

    @PostMapping()
    public R<String> add(@RequestBody Category category, HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        category.setCreateUser(id);
        category.setUpdateUser(id);

        Boolean add = categoryService.add(category);

        if (!add){
            return R.error("添加失败");
        }

        return R.success("添加成功");
    }

    @PutMapping()
    public R<String> update(@RequestBody Category category,HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        category.setUpdateUser(id);

        Boolean update = categoryService.update(category);

        if (!update){
            return R.error("修改失败");
        }

        return R.success("修改成功");
    }

    @DeleteMapping()
    public R<String> delete(long id){
        Boolean delete = categoryService.delete(id);

        if (!delete){
            return R.error("删除失败");
        }

        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<Category>> list(Integer type){
        List<Category> categories = categoryService.selectAllByType(type);

        R<List<Category>> success = R.success(categories);
        return success;
    }

}
